package com.demo.test.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.bluering.framework.common.base.BaseService;
import com.bluering.framework.common.log.Logger;
import com.demo.test.repository.mapper.OcsSysUserMapper;
import com.demo.test.repository.model.OcsSysUser;
import com.demo.test.service.IOcsSysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
@Transactional(rollbackFor=Exception.class,propagation=Propagation.REQUIRED)
public class OcsSysUserServiceImpl extends BaseService<OcsSysUser> implements IOcsSysUserService {
	
	private static Logger logger =Logger.getLogger(OcsSysUserServiceImpl.class);
	
	private static MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
	
	@Autowired
	private OcsSysUserMapper ocsSysUserMapper;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public PageInfo<OcsSysUser> queryForList(String userName, PageInfo page) {
		
		logger.info("queryForList 入参:{}",userName);
		
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		Example example = new Example(OcsSysUser.class);
		Criteria criteria =  example.createCriteria();
		//POJO里面的属性名称,不是数据库里面的字段名
		if(StringUtils.isNotEmpty(userName)){
			criteria.andLike("userName", "%"+userName+"%");
		}
		example.setOrderByClause(" id asc ");
		List<OcsSysUser> list = ocsSysUserMapper.selectByExample(example);
		
		PageInfo<OcsSysUser> p = new PageInfo<OcsSysUser>(list);
		logger.info(JSON.toJSONString(p));
		if(CollectionUtils.isEmpty(list)) {
			return null;
		}
		return p;
	}

	@Override
	public OcsSysUser queryByMobile(String mobile,String username) {
		List<OcsSysUser> users= ocsSysUserMapper.getUserByPhone(mobile,username);
		if(CollectionUtils.isEmpty(users)){
			return null;
		}
		return users.get(0);
	}

}
